package com.lie.prepare.multi_thread.even_checker;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by lie on 2018/4/9.
 * 关于lock的更多操作，可见{@link com.lie.prepare.multi_thread.mypractice.LockDemo
 * }
 */
public class MutexEvenGenerator extends IntGenerator {

    private Lock lock = new ReentrantLock();

    private int currentIntValue = 0;

    public int next() {
        lock.lock();
        try {
            ++currentIntValue;
            Thread.yield();//仅仅为了诱发并发问题
            ++currentIntValue;
            return currentIntValue;
        }finally {
            //当发生异常，你有机会在finally里做一些清理工作
            lock.unlock();
        }
    }
}
